package cn.com.bluemoon.daps.model.service.impl;

import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.common.exception.DapException;
import cn.com.bluemoon.daps.model.dto.ModelMetadataMappingDto;
import cn.com.bluemoon.daps.model.entity.DapModelMetadataMapping;
import cn.com.bluemoon.daps.model.mapper.DapModelMetadataMappingMapper;
import cn.com.bluemoon.daps.model.service.DapModelMetadataMappingService;
import cn.com.bluemoon.daps.model.vo.ModelMetadataMappingPageVo;
import cn.com.bluemoon.daps.model.vo.ModelMetadataMappingVo;
import cn.com.bluemoon.metadata.common.exception.DescribeException;
import cn.com.bluemoon.metadata.common.utils.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 模型和元数据映射表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-11-02
 */
@Slf4j
@Service
public class DapModelMetadataMappingServiceImpl extends ServiceImpl<DapModelMetadataMappingMapper, DapModelMetadataMapping>
        implements DapModelMetadataMappingService {

    public IPage<ModelMetadataMappingDto> listByPage(ModelMetadataMappingPageVo request){
        Page<ModelMetadataMappingDto> page = new Page<>(request.getPageNo(), request.getPageSize());
        return this.baseMapper.listByPage(page,request.getKeyword());
    }

    @Override
    public Boolean saveModelMetadataMapping(ModelMetadataMappingVo request){
        if(request.getMetadataSystemId().equals(request.getModelSystemId())){
            throw new DescribeException("默认同一系统下的模型表和元数据表是存在关系的了，不需要设置成强关联关系!", 500);
        }

        QueryWrapper<DapModelMetadataMapping> wrapper = new QueryWrapper<>();
        Long id = request.getId();
        String msg = "";
        if(Objects.isNull(id)){
            //新增
            wrapper.eq("model_system_id", request.getModelSystemId()).or().eq("metadata_system_id", request.getMetadataSystemId());
        }else {
            //修改
            wrapper.ne("id",id);
            wrapper.and(w->w.eq("model_system_id", request.getModelSystemId())
                    .or().eq("metadata_system_id", request.getMetadataSystemId()));
        }
        List<DapModelMetadataMapping> dapModelMetadataMappingList = baseMapper.selectList(wrapper);
        if(CollectionUtils.isNotEmpty(dapModelMetadataMappingList)){
            for (DapModelMetadataMapping modelMetadataMapping : dapModelMetadataMappingList) {
                if(modelMetadataMapping.getModelSystemId().equals(request.getModelSystemId())){
                    msg += "该模型表的系统已存在关系不允许创建关系；";
                }
                if(modelMetadataMapping.getMetadataSystemId().equals(request.getMetadataSystemId())){
                    msg += "该物理表的系统已存在关系不允许创建关系；";
                }
            }
            if(StringUtils.isNotEmpty(msg)){
                throw new DapException(msg, 500);
            }
        }

        DapModelMetadataMapping dapModelMetadataMapping = Objects.isNull(id)? new DapModelMetadataMapping():baseMapper.selectById(id);
        BeanUtils.copyPropertiesIgnoreNull(request,dapModelMetadataMapping);
        LocalDateTime now = LocalDateTime.now();
        dapModelMetadataMapping.setUpdateTime(now);
        dapModelMetadataMapping.setUpdateUserId(UserInfoHolder.getAccount());
        if(Objects.isNull(id)){
            dapModelMetadataMapping.setCreateTime(now);
            dapModelMetadataMapping.setCreateUserId(UserInfoHolder.getAccount());
            baseMapper.insert(dapModelMetadataMapping);
        }else {
            baseMapper.updateById(dapModelMetadataMapping);
        }

        return Boolean.TRUE;
    }

}
